// components/send-message/index.ts
import { upload } from "../../utils/upload";
const boxHeight = require("../../behavior/play-page-behavior");
import { addShortVideoComment } from "../../api/index";
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [boxHeight],
  properties: {
    isShowSendMsgPoup: {
      type: Boolean,
      value: false
    },
    CamID: {
      type: String,
      value: ""
    }
  },
  lifetimes: {
    ready() {
      wx.onKeyboardHeightChange(res => {
        this.setData({
          keyBoardHeight: res.height
        })
      });
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    // 发送消息 chart-room(为解决tab-bar在ios上层级覆盖问题)
    inputValue: "",
    isSendBarrage: false,
    replyId: "",
    isFocus: false,
    inputPlaceholder: "",
    keyBoardHeight: 0,
    upImgList: [] as any,
  },

  /**
   * 组件的方法列表
   */
  methods: {

    inputBlur() {
      if (this.data.inputValue == "") {
        this.setData({
          replyId: "",
          inputPlaceholder: "我来说两句~"
        })
      }
    },
    // 发送弹幕/评论
    sendMessage() {
      if (this.data.inputValue == "" && this.data.upImgList.length <= 0) return;
      const imgurl = this.data.upImgList.length > 0 ? this.data.upImgList[0].url : ""
      let detail = { isSendBarrage: this.data.isSendBarrage, msg: this.data.inputValue, replyId: this.data.replyId, ImgUrl: imgurl };
      if (detail.isSendBarrage) {
        // 发送弹幕
      } else {
        // 发送评论
        addShortVideoComment({
          "ParentId": detail.replyId,
          "ShortVideoId": this.properties.CamID,
          "Comment": detail.msg,
          "ImgUrl": detail.ImgUrl
        }).then((res: any) => {
          this.triggerEvent("setAddMsg", res.Data)
          if (detail.replyId == "") {
            this.triggerEvent("queryComment")
          }
        })
      };
      this.setData({
        inputValue: "",
        upImgList: [],
        replyId: "",
        inputPlaceholder: "",
        isFocus: false
      });
    },
    bindInputChange() {

    },
    // 切换弹幕/评论
    switchSendType() {
      this.setData({
        isSendBarrage: !this.data.isSendBarrage
      })
    },
    // 设置回复评论用户
    setReplyTarget(detail) {
      const { id, username } = detail;
      console.log(id, username);
      this.setData({
        inputPlaceholder: `@${username}:`,
        replyId: id,
        isFocus: true
      });
    },
    focusInput() {
      console.log("focusInput");
    },
    clickInput() {
      console.log("clickInput");
      setTimeout(() => {
        this.setData({
          isFocus: true,
        });
      }, 100);
      if (this.data.upImgList.length <= 0 && this.data.inputValue.length <= 0) {
        this.setData({
          inputPlaceholder: '',
          replyId: '',
        });
      }
    },
    loseFocus() {
      setTimeout(() => {
        this.setData({
          isFocus: false
        });
        // if (this.data.upImgList.length <= 0 && this.data.inputValue.length <= 0) {
        //   this.setData({
        //     inputPlaceholder: '',
        //     replyId: '',
        //   });
        // }
      }, 100);
    },
    // 评论图片上传
    imgAfterRead(e) {
      let file = e.detail.file;
      let type = e.currentTarget.dataset.uptype;
      upload({ file, uploadType: type }).then(url => {
        let upImgList = this.data.upImgList
        upImgList.push({ url });
        this.setData({ upImgList });
        this.clickInput()
      })
    },
    deleteFile() {
      this.setData({ upImgList: [] });
    },
  }
})
export { }